﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace AllegroWebAPI.LocalDB
{
    public class LocalData 
    {
        private const string catFile = "Categories.xml";
        private const string staFile = "States.xml";
        private const string shiFile = "Shipments.xml";
        private const string forFile = "Formfields.xml";

        private string path = "";
        GenericList<CatInfoType> categiroriesContainer;
        GenericList<StateInfoStruct> statesContainer;
        GenericList<ShipmentDataStruct> shipmentContainer;
        GenericList<SellFormType> formfieldsContainer;

        public IEnumerable<CatInfoType> Categories { 
            get {
                return categiroriesContainer.Items; 
            } 
        }

        public IEnumerable<StateInfoStruct> States
        {
            get {
                return statesContainer.Items;
            }
        }

        public IEnumerable<ShipmentDataStruct> Shipments
        {
            get {
                return shipmentContainer.Items;
            }
        }

        public IEnumerable<SellFormType> Formfields
        {
            get {
                return formfieldsContainer.Items;
            }
        }

        /// <summary>
        /// Pobiera pola formularza dla danej kategorii
        /// </summary>
        /// <param name="catId"></param>
        /// <returns></returns>
        public IEnumerable<SellFormType> GetFormfields(int catId)
        { 
            List<SellFormType> r = Formfields.Where(p => p.sellFormCat == 0).ToList();
            int cc = catId;

            while (cc != 0)
            {
                r.AddRange(Formfields.Where(p => p.sellFormCat == cc));
                cc = Categories.Where(p => p.catId == cc).FirstOrDefault().catParent;
            }
            return r;
        }

        /// <summary>
        /// Lokalna baza danych
        /// </summary>
        /// <param name="a"></param>
        public LocalData(string localDbPath)
        {
            categiroriesContainer = new GenericList<CatInfoType>();
            if (File.Exists(Path.Combine(path, catFile)))
            {
                categiroriesContainer.LoadFromFile(Path.Combine(path, catFile));
            }

            statesContainer = new GenericList<StateInfoStruct>();
            if (File.Exists(Path.Combine(path, staFile)))
            {
                statesContainer.LoadFromFile(Path.Combine(path, staFile));
            }

            shipmentContainer = new GenericList<ShipmentDataStruct>();
            if (File.Exists(Path.Combine(path, shiFile)))
            {
                shipmentContainer.LoadFromFile(Path.Combine(path, shiFile));
            }


            formfieldsContainer = new GenericList<SellFormType>();
            if (File.Exists(Path.Combine(path, forFile)))
            {
                formfieldsContainer.LoadFromFile(Path.Combine(path, forFile));
            }
        }

        public LocalData():this("")
        {

        }

        /// <summary>
        /// Pobiera dane z servera allegro i zapisuje na dysku.
        /// Przewidywana wielkość danych do ściągnięcia to 25MB
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        public bool Download(AllegroClient a)
        {
            CategoryTree ctree = new CategoryTree(a);
            categiroriesContainer.Items.Clear();
            foreach (var c in ctree.GetCatsData())
            {
                categiroriesContainer.Add(new CatInfoType()
                { 
                    catId = c.catId, 
                    catIsProductCatalogueEnabled = c.catIsProductCatalogueEnabled,
                    catName = c.catName,
                    catParent = c.catParent,
                    catPosition = c.catPosition
                });
            }
            categiroriesContainer.SaveToFile(Path.Combine(path, catFile));


            Varius va = new Varius(a);
            statesContainer.Items.Clear();
            foreach (var c in va.GetStatesInfo())
            {
                statesContainer.Add(new StateInfoStruct()
                {
                    stateId = c.stateId,
                    stateName = c.stateName
                });
            }
            statesContainer.SaveToFile(Path.Combine(path, staFile));


            shipmentContainer.Items.Clear();
            foreach (var c in va.GetShipmentData())
            {
                shipmentContainer.Add(new ShipmentDataStruct()
                {
                   shipmentId = c.shipmentId,
                   shipmentName = c.shipmentName,
                   shipmentTime = c.shipmentTime,
                   shipmentType = c.shipmentType
                });
            }
            shipmentContainer.SaveToFile(Path.Combine(path, shiFile));

            formfieldsContainer.Items.Clear();
            foreach (var c in va.GetSellFormFields())
            {
                formfieldsContainer.Add(new SellFormType()
                {
                    sellFormCat = c.sellFormCat,
                    sellFormDefValue = c.sellFormDefValue,
                    sellFormDesc = c.sellFormDesc,
                    sellFormFieldDesc = c.sellFormFieldDesc,
                    sellFormId = c.sellFormId,
                    sellFormLength = c.sellFormLength,
                    sellFormOpt = c.sellFormOpt,
                    sellFormOptions = c.sellFormOptions,
                    sellFormOptsValues = c.sellFormOptsValues,
                    sellFormParamId = c.sellFormParamId,
                    sellFormParamValues = c.sellFormParamValues,
                    sellFormParentId = c.sellFormParentId,
                    sellFormParentValue = c.sellFormParentValue,
                    sellFormPos = c.sellFormPos,
                    sellFormResType = c.sellFormResType,
                    sellFormTitle = c.sellFormTitle,
                    sellFormType = c.sellFormType,
                    sellFormUnit = c.sellFormUnit,
                    sellMaxValue = c.sellMaxValue,
                    sellMinValue = c.sellMinValue                    
                });
            }
            formfieldsContainer.SaveToFile(Path.Combine(path, forFile));

            return true;
        }

    }
}
